import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 加减法口算练习系统 v0.3
 * 新增功能：避免重复算式、控制加减法比例
 */
public class CalculationSystemV03 {
    private static Random random = new Random();
    private static Set<String> equationSet = new HashSet<>();
    
    /**
     * 生成随机数
     */
    public static int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }
    
    /**
     * 生成唯一算式（避免重复）
     */
    public static String[] generateUniqueEquation(int index) {
        String[] equationAndAnswer;
        String equationKey;
        int attempts = 0;
        final int MAX_ATTEMPTS = 100; // 防止无限循环
        
        do {
            equationAndAnswer = generateEquationWithAnswer();
            equationKey = equationAndAnswer[0].split("=")[0].trim(); // 使用算式部分作为key
            attempts++;
            
            if (attempts > MAX_ATTEMPTS) {
                // 如果尝试次数过多，重新初始化集合
                equationSet.clear();
                break;
            }
        } while (equationSet.contains(equationKey));
        
        equationSet.add(equationKey);
        equationAndAnswer[0] = String.format("%2d: %s", index, equationAndAnswer[0]);
        
        return equationAndAnswer;
    }
    
    /**
     * 生成单个算式（控制加减法比例为2:1）
     */
    private static String[] generateEquationWithAnswer() {
        int a, b, result;
        String operator;
        String[] equationAndAnswer = new String[2];
        
        // 控制加减法比例：2/3概率为加法，1/3概率为减法
        boolean isAddition = generateRandomNumber(1, 3) <= 2;
        
        if (isAddition) {
            // 加法：确保和不超过100
            do {
                a = generateRandomNumber(0, 100);
                b = generateRandomNumber(0, 100);
                result = a + b;
            } while (result > 100);
            operator = "+";
        } else {
            // 减法：确保差不小于0
            a = generateRandomNumber(0, 100);
            b = generateRandomNumber(0, a);
            result = a - b;
            operator = "-";
        }
        
        equationAndAnswer[0] = String.format("%2d %s %2d = %3d", a, operator, b, result);
        equationAndAnswer[1] = String.valueOf(result);
        
        return equationAndAnswer;
    }
    
    /**
     * 生成无重复的习题集
     */
    public static void generateUniqueExercises() {
        System.out.println("加减法口算练习系统 - v0.3");
        System.out.println("========================");
        System.out.println("无重复算式，加减法比例2:1：");
        System.out.println();
        
        for (int i = 1; i <= 50; i += 4) {
            for (int j = 0; j < 4 && (i + j) <= 50; j++) {
                String[] equation = generateUniqueEquation(i + j);
                System.out.print(equation[0] + "\t");
            }
            System.out.println();
        }
        
        // 统计信息
        System.out.println("\n统计信息：");
        System.out.println("总题数: 50");
        System.out.println("唯一算式数: " + equationSet.size());
    }
    
    public static void main(String[] args) {
        generateUniqueExercises();
    }
}